package com.woniu.venue.controller;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.woniu.exception.recruit.RecruitException;
import com.woniu.model.Result;
import com.woniu.util.*;
import com.woniu.venue.dto.recruit.RecruitDto;
import com.woniu.venue.dto.venueAdmin.VenueAdminDto;
import com.woniu.venue.form.recruit.*;
import com.woniu.venue.param.recruit.*;
import com.woniu.venue.service.RecruitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */

@Api(tags = "场馆招聘信息")
@RestController
@RequestMapping("/recruit")
public class RecruitController {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private RecruitService recruitService;
    @Autowired
    private OSSUtil ossUtil;

    @PostMapping("/upload")
    public Result uploadNoticeImage(MultipartFile image) throws Exception {
        //1.读取image中的文件内容
        InputStream inputStream = image.getInputStream();
        byte[] header = new byte[8];
        inputStream.read(header, 0, 8);
        //2.判断该文件的类型是否为图片
        if (!(FileUtil.isJpg(header) || FileUtil.isPng(header))) {
            throw new RecruitException(500,"上传图片失败");
        }
        //3.生成新的图片名称
        String prefix = UUID.randomUUID().toString().replaceAll("-", "");
        String fileName = prefix + "." + (FileUtil.isJpg(header) ? "jpg" : "png");
        //4.调用API上传
        String url = ossUtil.uploadFile(image.getInputStream(), fileName,"dmy001234");
        return Result.getResult().setMsg("上传公告封面成功").setData(url);
    }

//
    @ApiOperation("修改招聘信息")
    @PostMapping("/update")
    public Result update(@RequestBody UpdateRecruitFrom from) throws Exception {
        RecruitDto recruitDto= recruitService.updateRecruit(BeanUtil.toBean(from, UpdateRecruitParam.class));
        return Result.getResult().setMsg("成功").setData(recruitDto);
    }

    @ApiOperation("新增招聘信息")
    @PostMapping("/add")
    public Result addRecruit(@RequestBody AddRecruitFrom from) throws Exception {
        RecruitDto recruitDto=  recruitService.addRecruit(BeanUtil.toBean(from, AddRecruitParam.class));
        return Result.getResult().setMsg("新增招聘成功成功").setData(recruitDto);
    }

    @ApiOperation("删除")
    @PostMapping("/delete/recruitId")
    public Result deleteRecruit(@PathVariable("recruitId") Integer recruitId) throws Exception {
        recruitService.deleteRecruit(recruitId);
        return Result.getResult().setMsg("成功");
    }

    @ApiOperation("加载所有招聘信息")
    @PostMapping("/search")
    public Result searchRecruit(@RequestBody SearchRecruitFrom from) throws Exception {
        PageInfo<RecruitDto> pageInfo = recruitService.searchRecruit(BeanUtil.toBean(from, SearchRecruitParam.class));
        return Result.getResult().setData(pageInfo).setMsg("成功");
    }

    @ApiOperation("加载所有招聘信息 for venueAdmin")
    @PostMapping("/load.vapi")
    public Result loadRecruit(@RequestBody LoadRecruitFrom from) throws Exception {
        PageInfo<RecruitDto> pageInfo = recruitService.loadRecruit(BeanUtil.toBean(from, LoadRecruitParam.class));
        return Result.getResult().setData(pageInfo).setMsg("成功");
    }

    /**
     * 教练查看招聘信息
     *
     * @return
     * @throws Exception
     */
    @ApiOperation("教练搜索招聘信息")
    @PostMapping("/loadForCoach")
    public Result loadForCoach(@RequestBody LoadRecruitForCoachFrom from) throws Exception {
        PageInfo<RecruitDto> pageInfo = recruitService.loadForCoach(BeanUtil.toBean(from, LoadRecruitForCoachParam.class));
        return Result.getResult().setData(pageInfo).setMsg("成功");
    }


    @ApiOperation("展示招聘信息详情")
    @GetMapping("/showRecruit/{id}")
    public Result showRecruit(@PathVariable("id") Integer id) throws Exception {
        RecruitDto recruitDto= recruitService.showRecruit(id);
        return Result.getResult().setData(recruitDto).setMsg("成功");
    }

}

